<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>

<body>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashBoard">首页</a>
      <a href="/itemLists">商品管理</a>
      <a><cite>增加商品</cite></a>
    </span>
    <form class="layui-form" action="" style="max-width: 800px;margin-top: 20px">
      <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
          <input type="text" name="name" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
          <input type="number" name="price" min="0" required lay-verify="required" placeholder="请输入商品价格"
            autocomplete="off" class="layui-input">
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">上下架</label>
        <div class="layui-input-block">
          <input type="checkbox" name="onoff" checked lay-text="上架|下架" lay-skin="switch">
        </div>
      </div>

      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
          <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
      </div>

      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">封面图片</label>
        <div class="layui-input-block">
          <button type="button" class="layui-btn" id="upload">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
          <img src="" width="100" id="img" alt="">
        </div>
      </div>

      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
          <!-- 富文本编辑器 -->
          <div id="content"></div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="addItem">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>

  </div>

  <script src="/static/lib/layui/layui.js"></script>
  <script src="/static/lib/wangEditor.js"></script>
  <script>
    //Demo
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
      var form = layui.form;
      var $ = layui.jquery;
      var layer = layui.layer;
      var upload = layui.upload;
      var thumb = '';
      // 初始化文件上传插件
      //执行实例
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        , url: '/upload/' //上传接口
        ,field: 'file' // 上传字段名
        , done: function (res) {
          //上传完毕回调
          console.log(res);
          layer.msg('上传成功');
          $('#img').attr('src', res.data.thumb);
          thumb = res.data.thumb;
        }
        , error: function () {
          //请求异常回调
        }
      });
      // 初始化富文本编辑器
      const E = window.wangEditor;
      const editor = new E('#content');
      editor.create();
      //监听提交
      form.on('submit(addItem)', function (data) {
        // 提交数据 对象
        let { field } = data;

        field.onoff = field.onoff ? true : false;
        // 增加 封面图片字段
        field.thumb = thumb;
        // 获取富文本编辑器 输入内容
        field.content = editor.txt.html();
        // 发送ajax请求增加商品
        $.ajax({
          url: '/addItem',
          type: 'post',
          data: field,
          success: function (res) {
            if (res.code === 0) {
              // 成功
              layer.msg(res.msg, {
                icon: 1,
                time: 2000
              }, function () {
                history.go(0);
              })
            } else {
              // 失败
            }
          }
        }) 
        return false;
      });
    });
  </script>
</body>

</html>